package com.common.www.controller.project;

import com.alibaba.fastjson.JSON;
import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.project.Project;
import com.common.www.bean.vo.ProjectVo;
import com.common.www.controller.base.DefaultCrudController;
import com.common.www.data.NetCode;
import com.common.www.service.project.ProjectService;
import com.common.www.utils.ParamsUtils;
import com.common.www.utils.R;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


@CrossOrigin
@RestController
@RequestMapping(value = {"mgt/project"})
@Api(tags = "MGT 公司管理接口")
public class MGTProjectController extends DefaultCrudController<Project, Long> {
    @Autowired
    private ProjectService service;


    protected IBaseService getService() {
        return service;
    }
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/saveByJson", method = RequestMethod.POST)
    public Result saveByJson(String json) {
        ProjectVo projectVo = JSON.parseObject(json, ProjectVo.class);
        Project project = null;
        if (projectVo.getId() == null) {
            project = new Project();
        } else {
            project = service.getByIdLogic(projectVo.getId());
        }
        BeanUtils.copyProperties(projectVo,project);
        service.save(project);
        return R.ok();
    }

    @ApiOperation("批量删除启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "String", defaultValue = ""),
            @ApiImplicitParam(name = "operate", value = "1.删除 2.启用", defaultValue = ""),
    })
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public Result batchDelete(Long[] ids, Integer operate) {
        if (ParamsUtils.isInputEmpty(ids, operate)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        for (int i = 0; i < ids.length; i++) {
            Project project = service.findOne(ids[i]);
            if(project!=null){
                project.setFlag(0);
                service.save(project);
            }
        }
        return R.ok();
    }


    @ApiOperation(value = "逻辑删除", notes = "参数为JSON数据对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long", paramType = "query"),
    })
    @RequestMapping(value = "/deleteByLogic", method = RequestMethod.POST)
    public Result deleteByLogic(Long id) {
//        service.deleteById(id);
        return R.ok();
    }

    @ApiOperation(value = "逻辑删除批量", notes = "参数为JSON数据对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "8,28", dataType = "long", paramType = "query"),
    })
    @RequestMapping(value = "/deletesByLogic", method = RequestMethod.POST)
    public Result deletesByLogic(Long[] ids) {
//        service.deleteByIds(ids);
        return R.ok();
    }



}

